﻿using DTOs;
using RBAC.Domain.Service.Interfae;
using RBAC.Repository.Interfae;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RBAC.Domain.Service.Impl
{
    public class RoleMenuService : IRoleMenuService
    {
        /// <summary>
        /// 角色与菜单仓储
        /// </summary>
        private readonly IRoleMenuRepository _roleMenuRepository;
        /// <summary>
        /// 角色仓储
        /// </summary>
        private readonly IRoleRepository _roleRepository;
        /// <summary>
        /// 菜单
        /// </summary>
        private readonly IMenuRepository _menuRepository;
        public RoleMenuService(IRoleMenuRepository roleMenuRepository,IRoleRepository roleRepository, IMenuRepository menuRepository)
        {
            _roleMenuRepository = roleMenuRepository;
            _roleRepository = roleRepository;
            _menuRepository = menuRepository;
        }
        /// <summary>
        /// 添加角色菜单的关系
        /// </summary>
        /// <param name="roleMenu"></param>
        /// <returns></returns>
        public async Task<int> AddRoleMenu(RoleMenu roleMenu)
        {
           return await _roleMenuRepository.AddAsync(roleMenu);
        }
        /// <summary>
        /// 添加角色与菜单关系（根据角色ID，多选菜单ID）
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="MenuId"></param>
        /// <returns></returns>
        public async Task<int> AddRoleMenu(int roleId, List<int> MenuId)
        {
            foreach (var item in MenuId)
            {
                var res = new RoleMenu();
                res.roleId = roleId;
                res.MenuId = item;
                await _roleMenuRepository.AddAsync(res);
            }
            return 1;
        }

        ///// <summary>
        ///// 添加
        ///// </summary>
        ///// <param name="roleMenuDto"></param>
        ///// <returns></returns>
        //public async Task<int> GetAdd(RoleMenuDto roleMenuDto)
        //{
        //    foreach (var item in roleMenuDto.MenuId)
        //    {
        //        var list = await _roleMenuRepository.GetAsync(d => d.roleId == roleMenuDto.roleId &&
        //        d.MenuId == item);

        //    }
        //}

        /// <summary>
        ///菜单树形
        /// </summary>
        /// <param name="rid"></param>
        /// <returns></returns>
        public async Task<List<Menument>> GetMenuAsync(int rid=0)
        {
            //显示父ID下的菜单名称
           var list=await _menuRepository.GetAsync(d=>d.MenuPid==rid);
        
            //转换树形格式
            var listobj=list.ToList().Select(  a =>new Menument
            {
                Id=a.MenuId,
                label=a.MenuName,
                children= GetMenuAsync(a.MenuId).Result

            }).ToList();    
            return listobj;
        }
        /// <summary>
        /// 根据角色ID反填菜单
        /// </summary>
        /// <param name="rid"></param>
        /// <returns></returns>
        public async Task<List<RoleMenu>> GetMenuValue(int rid)
        {
            var list=(from a in await _roleMenuRepository.GetAllAsync()
                     join b in await _menuRepository.GetAllAsync()
                     on a.MenuId equals b.MenuId
                     join c in await _roleRepository.GetAllAsync()
                     on a.roleId equals c.roleId
                     where c.roleId == rid
                     select new RoleMenu
                     {
                       
                         MenuId=b.MenuId,
                       
                     }).ToList();
            return list;

        }


        /// <summary>
        /// 角色树形
        /// </summary>
        /// <returns></returns>
        public async Task<List<Menument>> GetRoleAsync()
        {
            var rolelist = from a in await _roleRepository.GetAllAsync()
                           where !a.IsDel
                           select a;
            var roleobj=rolelist.ToList().Select(d=>new Menument
            {
                Id=d.roleId,
                label=d.roleName,
                children=new List<Menument>()
            }).ToList();
            return roleobj;
           
        }
    }
}
